Quero obter texto da web, escrevi código, uso indexOf e subString, mas não funciona. int index = response_str.indexOf ("Endereço IP remoto:"); índice + = "Endereço IP remoto:". comprimento (); índice + = "
". length ();
int end = response_str.indexOf ("
", índice); String strIP = response_str.substring (índice, fim); Log.d ("", "Olá" + strIP); E este é um texto de amostra, eu quero obter 49.156.53.152
Endereço IP remoto:
** 49.156.53.152 **
UserAgent:
Mozilla / 5.0 (Windows NT 6.1; WOW64) AppleWebKit / 537.11 (KHTML, como Gecko) Chrome / 23.0.1271.64 Safari / 537.11
2020-12-21 08:58:07
Você pode converter a página html em um sting. e usar operações regex / string para obter os dados necessários experimentar { if (! url_text.getText (). toString (). trim (). equalsIgnoreCase ("")) { textView.setText (""); Cliente HttpClient = novo DefaultHttpClient (); Solicitação HttpGet = new HttpGet (url_text.getText (). ToString ()); // Obtenha a resposta ResponseHandlerresponseHandler = new BasicResponseHandler (); String response_str = client.execute (request, responseHandler); textView.setText (response_str); }outro{ Toast.makeText (getApplicationContext (), "String de URL vazia.", Toast.LENGTH_LONG) .show (); } } catch (exceção e) { System.out.println ("Ocorreu algum erro."); textView.setText (e.getMessage ()); } talvez a maneira mais fácil seja usar a função sting.split String [] separada = response_str.split ("**"); separados [0]; // parte antes de ** separados [1]; // sua string de ip necessária separados [2]; // parte após a segunda ** | Você deve fazer uso do objeto de interface Javascript com Java para buscar todos os dados da página da web Html. final WebView webview = (WebView) findViewById (R.id.browser); webview.getSettings (). setJavaScriptEnabled (true); webview.addJavascriptInterface (new MyJavaScriptInterface (this), "HtmlViewer"); webview.setWebViewClient (new WebViewClient () { @Sobrepor public void onPageFinished (visualização WebView, URL da string) { webview.loadUrl ("javascript: window.HtmlViewer.showHTML" + "('' + document.getElementsByTagName ('html') [0] .innerHTML + '');"); } }); webview.loadUrl ("http://android-in-action.com/index.php?post/" + "Erros e bugs comuns e como resolvê-los evitá-los"); } class MyJavaScriptInterface { Contexto privado ctx; MyJavaScriptInterface (Context ctx) { this.ctx = ctx; } public void showHTML (String html) { new AlertDialog.Builder (ctx) .setTitle ("HTML"). setMessage (html) .setPositiveButton (android.R.string.ok, null) .setCancelable (false) .create (). show (); } } | Você deve tentar usar isso. myString = String.subString (" ", YOUR_ORIGINAL_STRING.len); String necessária = myString.endsWith (""); | Você pode usar JSoup. Documento doc = Jsoup.connect (URL_TO_HTML_PAGE) .get (); Elementos newsHeadlines = doc.getElementsByAttribute ("value1"); String ip = newsHeadlines [0] .text (). Split ("**") [1]; A última linha é baseada na postagem do Greezer. Eu pessoalmente o substituiria por um padrão regex simples que corresponda a todos os endereços IP possíveis. Algo como: \ b (25 [0-5] | 2 [0-4] [0-9] | [01]? [0-9] [0-9]?) \. (25 [0-5] | 2 [ 0-4] [0-9] | [01]? [0-9] [0-9]?) \. (25 [0-5] | 2 [0-4] [0-9] | [01 ]? [0-9] [0-9]?) \. (25 [0-5] | 2 [0-4] [0-9] | [01]? [0-9] [0-9] ?) \ b | sua resposta StackExchange.ifUsing ("editor", function () { StackExchange.using ("externalEditor", function () { StackExchange.using ("snippets", function () { StackExchange.snippets.init (); }); }); }, "partes de codigo"); StackExchange.ready (function () { var channelOptions = { tags: "" .split (""), id: "1" }; initTagRenderer ("". split (""), "" .split (""), channelOptions); StackExchange.using ("externalEditor", function () { // Tem que disparar o editor após os snippets, se os snippets estiverem habilitados if (StackExchange.settings.snippets.snippetsEnabled) { StackExchange.using ("snippets", function () { createEditor (); }); } outro { createEditor (); } }); function createEditor () { StackExchange.prepareEditor ({ useStacksEditor: false, heartbeatType: 'answer', autoActivateHeartbeat: false, convertImagesToLinks: true, noModals: true, showLowRepImageUploadWarning: true, reputaçãoToPostImages: 10, bindNavPrevention: true, postfix: "", imageUploader: { brandingHtml: "Powered by \ u003ca href = \" https: //imgur.com/ \ "\ u003e \ u003csvg class = \" svg-icon \ "width = \" 50 \ "height = \" 18 \ "viewBox = \ "0 0 50 18 \" fill = \ "none \" xmlns = \ "http: //www.w3.org/2000/svg \" \ u003e \ u003cpath d = \ "M46.1709 9.17788C46.1709 8.26454 46,2665 7,94324 47,1084 7.58816C47.4091 7,46349 47,7169 7,36433 48,0099 7.26993C48.9099 6,97997 49,672 6,73443 49,672 5.93063C49.672 5,22043 48,9832 4,61182 48,1414 4.61182C47.4335 4,61182 46,7256 4,91628 46,0943 5.50789C45.7307 4,9328 45,2525 4,66231 44,6595 4.66231C43.6264 4,66231 43,1481 5,28821 43.1481 6.59048V11.9512C43.1481 13.2535 43.6264 13.8962 44.6595 13.8962C45.6924 13.8962 46.1709 13.2535 46.1709 11.9512V9.17788Z \ "/ \ u003e \ u003cpath d = \" M32.492 10.1439219 C32.492.492. 41.5985 12.6954 41.5985 10.1419V6.59049C41.5985 5.28821 41.1394 4.66232 40.1061 4.66232C39.0732 4.66232 38.5948 5.28821 38.5948 6.59049V9.60062C38.5948 10,85821 38.2696 11.5455 37.0454 11.520 11.545 11.5458.5948 5.28821 38.5455 11.5204 11.5204 11.5204 11.5458 11.5458. 521 35.4954 9.60062V6.59049C35.4954 5.28821 35.0173 4.66232 34.0034 4.66232C32.9703 4.66232 32.492 5.28821 32.492 6.59049V10.1419Z \ "/ \ u003e \ u003cpath fill-rule = \" evenodd \ "clip-rule = \" evenodd \ "clip-rule = \ "M25.6622 17.6335C27.8049 17.6335 29.3739 16.9402 30,2537 15.6379C30.8468 14.7755 30,9615 13.5579 30,9615 11.9512V6.59049C30.9615 5.28821 30.4833 4.66231 29.4502 4.66231C30.8468 14.7755 30,9615 13.5579 30,9615 11.9512V6.59049C30.9615 5.28821 30.4833 4.66231 29.4502 4.66231C30.8468 14.7755 30,9615 13.5579 30,9615 11.9512V6.59049C30.9615 5.28821 30.4833 4.66231 29.4502 4.66231C 28.1993 4.66231Você pode converter a página html em um sting. e usar operações regex / string para obter os dados necessários experimentar { if (! url_text.getText (). toString (). trim (). equalsIgnoreCase ("")) { textView.setText (""); Cliente HttpClient = novo DefaultHttpClient (); Solicitação HttpGet = new HttpGet (url_text.getText (). ToString ()); // Obtenha a resposta ResponseHandlerresponseHandler = new BasicResponseHandler (); String response_str = client.execute (request, responseHandler); textView.setText (response_str); }outro{ Toast.makeText (getApplicationContext (), "String de URL vazia.", Toast.LENGTH_LONG) .show (); } } catch (exceção e) { System.out.println ("Ocorreu algum erro."); textView.setText (e.getMessage ()); } talvez a maneira mais fácil seja usar a função sting.split String [] separada = response_str.split ("**"); separados [0]; // parte antes de ** separados [1]; // sua string de ip necessária separados [2]; // parte após a segunda ** | Você deve fazer uso do objeto de interface Javascript com Java para buscar todos os dados da página da web Html. final WebView webview = (WebView) findViewById (R.id.browser); webview.getSettings (). setJavaScriptEnabled (true); webview.addJavascriptInterface (new MyJavaScriptInterface (this), "HtmlViewer"); webview.setWebViewClient (new WebViewClient () { @Sobrepor public void onPageFinished (visualização WebView, URL da string) { webview.loadUrl ("javascript: window.HtmlViewer.showHTML" + "('' + document.getElementsByTagName ('html') [0] .innerHTML + '');"); } }); webview.loadUrl ("http://android-in-action.com/index.php?post/" + "Erros e bugs comuns e como resolvê-los evitá-los"); } class MyJavaScriptInterface { Contexto privado ctx; MyJavaScriptInterface (Context ctx) { this.ctx = ctx; } public void showHTML (String html) { new AlertDialog.Builder (ctx) .setTitle ("HTML"). setMessage (html) .setPositiveButton (android.R.string.ok, null) .setCancelable (false) .create (). show (); } } | Você deve tentar usar isso. myString = String.subString (" ", YOUR_ORIGINAL_STRING.len); String necessária = myString.endsWith (""); | Você pode usar JSoup. Documento doc = Jsoup.connect (URL_TO_HTML_PAGE) .get (); Elementos newsHeadlines = doc.getElementsByAttribute ("value1"); String ip = newsHeadlines [0] .text (). Split ("**") [1]; A última linha é baseada na postagem do Greezer. Eu pessoalmente o substituiria por um padrão regex simples que corresponda a todos os endereços IP possíveis. Algo como: \ b (25 [0-5] | 2 [0-4] [0-9] | [01]? [0-9] [0-9]?) \. (25 [0-5] | 2 [ 0-4] [0-9] | [01]? [0-9] [0-9]?) \. (25 [0-5] | 2 [0-4] [0-9] | [01 ]? [0-9] [0-9]?) \. (25 [0-5] | 2 [0-4] [0-9] | [01]? [0-9] [0-9] ?) \ b | sua resposta StackExchange.ifUsing ("editor", function () { StackExchange.using ("externalEditor", function () { StackExchange.using ("snippets", function () { StackExchange.snippets.init (); }); }); }, "partes de codigo"); StackExchange.ready (function () { var channelOptions = { tags: "" .split (""), id: "1" }; initTagRenderer ("". split (""), "" .split (""), channelOptions); StackExchange.using ("externalEditor", function () { // Tem que disparar o editor após os snippets, se os snippets estiverem habilitados if (StackExchange.settings.snippets.snippetsEnabled) { StackExchange.using ("snippets", function () { createEditor (); }); } outro { createEditor (); } }); function createEditor () { StackExchange.prepareEditor ({ useStacksEditor: false, heartbeatType: 'answer', autoActivateHeartbeat: false, convertImagesToLinks: true, noModals: true, showLowRepImageUploadWarning: true, reputaçãoToPostImages: 10, bindNavPrevention: true, postfix: "", imageUploader: { brandingHtml: "Powered by \ u003ca href = \" https: //imgur.com/ \ "\ u003e \ u003csvg class = \" svg-icon \ "width = \" 50 \ "height = \" 18 \ "viewBox = \ "0 0 50 18 \" fill = \ "none \" xmlns = \ "http: //www.w3.org/2000/svg \" \ u003e \ u003cpath d = \ "M46.1709 9.17788C46.1709 8.26454 46,2665 7,94324 47,1084 7.58816C47.4091 7,46349 47,7169 7,36433 48,0099 7.26993C48.9099 6,97997 49,672 6,73443 49,672 5.93063C49.672 5,22043 48,9832 4,61182 48,1414 4.61182C47.4335 4,61182 46,7256 4,91628 46,0943 5.50789C45.7307 4,9328 45,2525 4,66231 44,6595 4.66231C43.6264 4,66231 43,1481 5,28821 43.1481 6.59048V11.9512C43.1481 13.2535 43.6264 13.8962 44.6595 13.8962C45.6924 13.8962 46.1709 13.2535 46.1709 11.9512V9.17788Z \ "/ \ u003e \ u003cpath d = \" M32.492 10.1439219 C32.492.492. 41.5985 12.6954 41.5985 10.1419V6.59049C41.5985 5.28821 41.1394 4.66232 40.1061 4.66232C39.0732 4.66232 38.5948 5.28821 38.5948 6.59049V9.60062C38.5948 10,85821 38.2696 11.5455 37.0454 11.520 11.545 11.5458.5948 5.28821 38.5455 11.5204 11.5204 11.5204 11.5458 11.5458. 521 35.4954 9.60062V6.59049C35.4954 5.28821 35.0173 4.66232 34.0034 4.66232C32.9703 4.66232 32.492 5.28821 32.492 6.59049V10.1419Z \ "/ \ u003e \ u003cpath fill-rule = \" evenodd \ "clip-rule = \" evenodd \ "clip-rule = \ "M25.6622 17.6335C27.8049 17.6335 29.3739 16.9402 30,2537 15.6379C30.8468 14.7755 30,9615 13.5579 30,9615 11.9512V6.59049C30.9615 5.28821 30.4833 4.66231 29.4502 4.66231C30.8468 14.7755 30,9615 13.5579 30,9615 11.9512V6.59049C30.9615 5.28821 30.4833 4.66231 29.4502 4.66231C30.8468 14.7755 30,9615 13.5579 30,9615 11.9512V6.59049C30.9615 5.28821 30.4833 4.66231 29.4502 4.66231C 28.1993 4.66231